//
//  StringHelp.swift
//  weile
//
//  Created by LKM on 16/10/13.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit
import Foundation

class StringHelp: NSObject {
    
    
    class func toJson(o:Any?)->String?{
    
        if(o == nil){
            return nil
        }
        //利用自带的json库转换成Data
        //如果设置options为JSONSerialization.WritingOptions.prettyPrinted，则打印格式更好阅读
        let data = try? JSONSerialization.data(withJSONObject: o!, options: [])
        
        if ( data == nil){
            return nil;
        }
        //Data转换成String打印输出
        let str = String(data:data!, encoding: String.Encoding.utf8)

        return str;
    }
    
    class func size( str:String?) -> Int{
        if str == nil{
            return 0;
        }
        return str!.characters.count;
        
    }

    //LKMLog("\(StringHelp.substring(str: "123456789", sIndex: 2, length: 2))")//34
    class func substring( str:String?,sIndex:Int = 0,length:Int) -> String{
        
        if let strs = str{
            
            
            let index = strs.index(strs.startIndex, offsetBy: sIndex)
            
            let eindex = strs.index(strs.startIndex, offsetBy: sIndex+length)
            
            return strs.substring( with: Range<String.Index>.init(  uncheckedBounds: (lower: index, upper: eindex) )  )

        }else{
            return ""
        }
        
    }
    //LKMLog("\(StringHelp.substring(str: "123456789", afterIndex: 2, length: 2))")//67
    class func substring( str:String?,afterIndex:Int = 0,length:Int) -> String{
        
        if let strs = str{
            let index = strs.index(strs.endIndex, offsetBy: -1 * afterIndex)
            
            let eindex = strs.index(strs.endIndex, offsetBy: -1 * (afterIndex+length))
            
            return strs.substring( with: Range<String.Index>.init(  uncheckedBounds: (lower: eindex, upper: index) )  )
            
        }else{
            return ""
        }
        
    }
    
    class func trim(_ s:String?) -> String?{
        if s == nil{
            return s
        }
        
//        let ss = s!.trimmingCharacters(in: CharacterSet(charactersIn: "m")) 替换掉首尾的"m"
        return s!.trimmingCharacters(in: CharacterSet.whitespaces)
    }
    
    class func isEmpty(_ s:String? )->Bool{
        if s == nil{
            return true;
        }
        return s!.isEmpty;
    }

    
    class func split(_ s:String?,_ sp:String )->[String]{
        if s == nil{
            return [String]()
        }
        return s!.components(separatedBy: sp)
    }
    
    class func replaceAll(_ source:String?, old:String , new:String )->String{
        
        if source == nil{
            return ""
        }
        return source!.replacingOccurrences(of: old, with: new,  options: String.CompareOptions.literal, range: nil)
    }

    class func toData(_ s:String?)-> Data?{
        if s == nil{
            return nil
        }
        return s!.data(using: String.Encoding.utf8)
    }
    
    class func base64Encode(_ s:String?)->Data?{
        if s == nil{
            return nil
        }
        return GTMBase64.encode((s!).data(using: String.Encoding.utf8)) ;
    }
    
    class func base64Decode(_ s:String?)->Data?{
        if s == nil{
            return nil
        }
        return GTMBase64.decode((s!).data(using: String.Encoding.utf8)) ;
    }
}
